const AJAX = getApp().ajax,
      $wx = getApp().$wx,
      getColor = getApp().globalData.getColor;

Page({
  data:{
    getColor: getColor(),
    isPayment: false,
    remarkInfo: null,
    paymentType: {
      balanceMoney: 1,
      WeCahtMoney: 2
    },
    phone: null
  },

  onLoad(options){
    $wx.setNavColor(getColor().val);

    if(options.order_id){
      this.setData({ orderId: options.order_id });
    }

    this.setData({
      shopConfig: wx.getStorageSync('shopConfig')
    });
  },

  onShow(){
    this.createOrder();
  },

  onShareAppMessage(){
  },

  phoneInput(e){
    this.setData({
      phone: e.detail.value
    });
  },

  // 立即支付
  ImmediatePayment(){
    let _this = this, td = _this.data;

    if(!td.phone){

      $wx.showToast('请输入手机号');

    }else if(td.phone && !$wx.checkPhone(td.phone)){

      $wx.showToast('请输入正确的手机号');

    }else{

      wx.showModal({
        title: '提示',
        content: '请确认当前信息',
        success(res){
          if(res.confirm){
            _this.setData({
              isPayment: true
            });
          }
        }
      });
      
    }
  },

  // 加载失败回调
  loadFailedCallback(){
    console.log('yes');
  },

  // 备注
  remarkInput(e){
    let val = e.detail.value;

    this.setData({
      remarkInfo: val
    });
  },

  // 确认支付
  paymentConfirm(e){
    let _this = this, td = _this.data, 
        payment_info = e.detail.payment;
        
    if (!payment_info.type){
      $wx.showToast('请选择支付方式');
    }else{
      AJAX.addOrder({
        pay_type: td.paymentType[payment_info.type],
        address: td.shopConfig.shop_address,
        mobile: td.phone,
        price: td.orderInfo.car_total_price,
        order_id: td.orderId || null,
        remark: td.remarkInfo
      }, (status, data, msg) => {
        if (status) {
          _this.payOrder({
            pay_type: data.pay_type,
            order_id: data.order_id
          });
        } else {
          $wx.showToast(msg);
        }
      });
    }
  },

  /*创建订单*/
  createOrder() {
    let _this = this, td = _this.data;
    wx.showNavigationBarLoading();
    
    AJAX.createOrder({
      order_id: td.orderId || null
    }, (status, data, msg) => {
      wx.hideNavigationBarLoading();
      if(status){
        _this.setData({
          orderInfo: data,
          phone: data.user_phone
        });
      }else{
        $wx.showToast(msg);
      }
    });
  },

  /**
   * 支付
   * @param pay_type
   * @param order_id
   * */ 
  payOrder(params = {}){
    let _this = this;
    wx.showLoading({
      title: '支付中...'
    });
    
    AJAX.payOrder(params, (status, data, msg)=>{
      wx.hideLoading();
      
      if(status){
        if (params.pay_type == 1){
          $wx.showToast(msg);
         
          setTimeout(callBack => {
            wx.redirectTo({
              url: '/pages/other-pages/order-detail/order-detail?order_id=' + data.order_id,
              success() {
                _this.setData({
                  isPayment: false
                });
              }
            });
          }, 1500);
        } else if (params.pay_type == 2){
          wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data.package,
            signType: data.signType,
            paySign: data.paySign,
            success(res) {
              $wx.showToast('支付成功');

              setTimeout(callBack => {
                wx.redirectTo({
                  url: '/pages/other-pages/order-detail/order-detail?order_id=' + data.order_id,
                  success() {
                    _this.setData({
                      isPayment: false
                    });
                  }
                });
              }, 1500);
            },
            fail(res) {
              $wx.showToast('支付失败');
              setTimeout(callBack => {
                wx.redirectTo({
                  url: '/pages/other-pages/order-detail/order-detail?order_id=' + data.order_id,
                  success() {
                    _this.setData({
                      isPayment: false
                    });
                  }
                });
              }, 1500);
            }
          });
        }
      }else{
        $wx.showToast(msg);
      }
    });
  }
  
});